package com.android.tiango.baba.act;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.imageFilters.ComicFilter;
import com.android.tiango.baba.imageFilters.FeatherFilter;
import com.android.tiango.baba.imageFilters.GlowingEdgeFilter;
import com.android.tiango.baba.imageFilters.IceFilter;
import com.android.tiango.baba.imageFilters.LomoFilter;
import com.android.tiango.baba.imageFilters.MoltenFilter;
import com.android.tiango.baba.imageFilters.SoftGlowFilter;
import com.android.tiango.baba.imageFilters.ZoomBlurFilter;
import com.android.tiango.baba.util.ImageCache;
import com.android.tiango.baba.util.ImageUtil;

/**
 * 滤镜
 * 
 * @author suip
 * 
 */
public class ActFilter extends BaBaseActivity implements OnClickListener {

	private ImageView imageView2;
	private ImageView mYuanTu, mHuiDu, mBingDong, mRongZhu, mLianHuanHua,
			mMeiBai, mZhaoBian, mYuHua, mSuoFang, mLoMo;
	private Bitmap mBitmap, tmpBitmap;
	private String[] key = { "IceFilter", "MoltenFilter", "ComicFilter",
			"SoftGlowFilter", "GlowingEdgeFilter", "FeatherFilter",
			"ZoomBlurFilter", "LomoFilter","HuiDu" };
	private ImageView mBack,mSure;
	private static String FITERNAME;
	public static int HandlerWhat=2121;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_filter);

		initView();

	}

	@SuppressWarnings("deprecation")
	private void initView() {
		// TODO Auto-generated method stub

		String filename2 = (String) getIntent().getExtras().get("name");

		mBitmap = BitmapFactory.decodeFile(filename2);

		tmpBitmap = mBitmap;

		imageView2 = (ImageView) findViewById(R.id.iv_picture);
		mYuanTu = (ImageView) findViewById(R.id.iv_yuantu);
		mHuiDu = (ImageView) findViewById(R.id.iv_huidu);
		mBingDong = (ImageView) findViewById(R.id.iv_bingdong);
		mRongZhu = (ImageView) findViewById(R.id.iv_rongzhu);
		mLianHuanHua = (ImageView) findViewById(R.id.iv_lianhuanhua);
		mMeiBai = (ImageView) findViewById(R.id.iv_meibai);
		mZhaoBian = (ImageView) findViewById(R.id.iv_zhaoliangbianyuan);
		mYuHua = (ImageView) findViewById(R.id.iv_yuhua);
		mSuoFang = (ImageView) findViewById(R.id.iv_suofang);
		mLoMo = (ImageView) findViewById(R.id.iv_lomo);
		
		mBack=(ImageView) findViewById(R.id.bt_back);
		mSure=(ImageView) findViewById(R.id.bt_sure);
		
		//imageView2.setImageBitmap(mBitmap);
		mYuanTu.setBackgroundResource(R.drawable.bg_fiter2);
		imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(mBitmap));
		
		mHuiDu.setOnClickListener(this);
		mBingDong.setOnClickListener(this);
		mYuanTu.setOnClickListener(this);
		mRongZhu.setOnClickListener(this);
		mLianHuanHua.setOnClickListener(this);
		mMeiBai.setOnClickListener(this);
		mZhaoBian.setOnClickListener(this);
		mYuHua.setOnClickListener(this);
		mSuoFang.setOnClickListener(this);
		mLoMo.setOnClickListener(this);
		mBack.setOnClickListener(this);
		mSure.setOnClickListener(this);
		
		setSmallBitmap();
	}

	private void setSmallBitmap() {
		// TODO Auto-generated method stub
		Bitmap smallBitmap = ImageUtil.zoomBitmap(mBitmap, 20, 20);
		mYuanTu.setImageBitmap(smallBitmap);
		mHuiDu.setImageBitmap(ImageUtil.toGrayscale(smallBitmap));
		mBingDong.setImageBitmap(new IceFilter(smallBitmap).imageProcess()
				.getDstBitmap());
		mRongZhu.setImageBitmap(new MoltenFilter(smallBitmap).imageProcess()
				.getDstBitmap());
		mLianHuanHua.setImageBitmap(new ComicFilter(smallBitmap).imageProcess()
				.getDstBitmap());
		mMeiBai.setImageBitmap(new SoftGlowFilter(smallBitmap).imageProcess()
				.getDstBitmap());
		mZhaoBian.setImageBitmap(new GlowingEdgeFilter(smallBitmap)
				.imageProcess().getDstBitmap());
		mYuHua.setImageBitmap(new FeatherFilter(smallBitmap).imageProcess()
				.getDstBitmap());
		mSuoFang.setImageBitmap(new ZoomBlurFilter(smallBitmap, 30)
				.imageProcess().getDstBitmap());
		mLoMo.setImageBitmap(new LomoFilter(smallBitmap).imageProcess()
				.getDstBitmap());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.bt_back:
			showActivity(ActPhotoCamera.class, true);
			break;
		case R.id.bt_sure:
			load();
			finish();
			break;
		case R.id.iv_huidu:
			//imageView2.setImageBitmap(ImageUtil.toGrayscale(mBitmap));
			
			//mHuiDu.setBackgroundResource(R.drawable.bg_fiter2);
			if (ImageCache.get(key[8]) != null) {
				tmpBitmap = ImageCache.get(key[8]);
				//imageView2.setImageBitmap(tmpBitmap);
				imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
				break;
			}
			new Thread() {
				public void run() {
					mHandler.sendEmptyMessage(PROGRESS_WAIT_VISIBLE);
					Bitmap tmpBitmap = ImageUtil.toGrayscale(mBitmap);
					Message msg = new Message();
					msg.obj = tmpBitmap;
					msg.arg1 = 8;
					msg.what = PROGRESS_WAIT_GONE;
					mHandler.sendMessage(msg);
				}
			}.start();
			break;
		case R.id.iv_bingdong:
			if (ImageCache.get(key[0]) != null) {
				tmpBitmap = ImageCache.get(key[0]);
				//imageView2.setImageBitmap(tmpBitmap);
				imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
				
				break;
			}
			new Thread() {
				public void run() {
					mHandler.sendEmptyMessage(PROGRESS_WAIT_VISIBLE);
					Bitmap tmpBitmap = new IceFilter(mBitmap).imageProcess()
							.getDstBitmap();
					Message msg = new Message();
					msg.obj = tmpBitmap;
					msg.arg1 = 0;
					msg.what = PROGRESS_WAIT_GONE;
					mHandler.sendMessage(msg);
				}
			}.start();
			//mBingDong.setBackgroundResource(R.drawable.bg_fiter2);
			break;

		case R.id.iv_rongzhu:

			if (ImageCache.get(key[1]) != null) {
				tmpBitmap = ImageCache.get(key[1]);
				//imageView2.setImageBitmap(tmpBitmap);
				imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
				break;
			}
			new Thread() {
				public void run() {
					mHandler.sendEmptyMessage(PROGRESS_WAIT_VISIBLE);
					Bitmap tmpBitmap = new MoltenFilter(mBitmap).imageProcess()
							.getDstBitmap();
					Message msg = new Message();
					msg.obj = tmpBitmap;
					msg.arg1 = 1;
					msg.what = PROGRESS_WAIT_GONE;
					mHandler.sendMessage(msg);
				}
			}.start();
			break;

		case R.id.iv_lianhuanhua:
			if (ImageCache.get(key[2]) != null) {
				tmpBitmap = ImageCache.get(key[2]);
				//imageView2.setImageBitmap(tmpBitmap);
				imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
				break;
			}
			new Thread() {
				public void run() {
					mHandler.sendEmptyMessage(PROGRESS_WAIT_VISIBLE);
					Bitmap tmpBitmap = new ComicFilter(mBitmap).imageProcess()
							.getDstBitmap();
					Message msg = new Message();
					msg.obj = tmpBitmap;
					msg.arg1 = 2;
					msg.what = PROGRESS_WAIT_GONE;
					mHandler.sendMessage(msg);
				}
			}.start();
			break;

		case R.id.iv_meibai:
			if (ImageCache.get(key[3]) != null) {
				tmpBitmap = ImageCache.get(key[3]);
				//imageView2.setImageBitmap(tmpBitmap);
				imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
				break;
			}
			new Thread() {
				public void run() {
					mHandler.sendEmptyMessage(PROGRESS_WAIT_VISIBLE);
					Bitmap tmpBitmap = new SoftGlowFilter(mBitmap, 10, 0.1f,
							0.1f).imageProcess().getDstBitmap();
					Message msg = new Message();
					msg.obj = tmpBitmap;
					msg.arg1 = 3;
					msg.what = PROGRESS_WAIT_GONE;
					mHandler.sendMessage(msg);
				}
			}.start();
			break;
		case R.id.iv_zhaoliangbianyuan:
			if (ImageCache.get(key[4]) != null) {
				tmpBitmap = ImageCache.get(key[4]);
				//imageView2.setImageBitmap(tmpBitmap);
				imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
				break;
			}
			new Thread() {
				public void run() {
					mHandler.sendEmptyMessage(PROGRESS_WAIT_VISIBLE);
					Bitmap tmpBitmap = new GlowingEdgeFilter(mBitmap)
							.imageProcess().getDstBitmap();
					Message msg = new Message();
					msg.obj = tmpBitmap;
					msg.arg1 = 4;
					msg.what = PROGRESS_WAIT_GONE;
					mHandler.sendMessage(msg);
				}
			}.start();
			break;
		case R.id.iv_yuhua:
			if (ImageCache.get(key[5]) != null) {
				tmpBitmap = ImageCache.get(key[5]);
				//imageView2.setImageBitmap(tmpBitmap);
				imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
				break;
			}
			new Thread() {
				public void run() {
					mHandler.sendEmptyMessage(PROGRESS_WAIT_VISIBLE);
					Bitmap tmpBitmap = new FeatherFilter(mBitmap)
							.imageProcess().getDstBitmap();
					Message msg = new Message();
					msg.obj = tmpBitmap;
					msg.arg1 = 5;
					msg.what = PROGRESS_WAIT_GONE;
					mHandler.sendMessage(msg);
				}
			}.start();
			break;

		case R.id.iv_suofang:
			if (ImageCache.get(key[6]) != null) {
				tmpBitmap = ImageCache.get(key[6]);
				//imageView2.setImageBitmap(tmpBitmap);
				imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
				break;
			}
			new Thread() {
				public void run() {
					mHandler.sendEmptyMessage(PROGRESS_WAIT_VISIBLE);
					Bitmap tmpBitmap = new ZoomBlurFilter(mBitmap, 30)
							.imageProcess().getDstBitmap();
					Message msg = new Message();
					msg.obj = tmpBitmap;
					msg.arg1 = 6;
					msg.what = PROGRESS_WAIT_GONE;
					mHandler.sendMessage(msg);
				}
			}.start();
			break;
		case R.id.iv_lomo:
			if (ImageCache.get(key[7]) != null) {
				tmpBitmap = ImageCache.get(key[7]);
				//imageView2.setImageBitmap(tmpBitmap);
				imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
				break;
			}
			new Thread() {
				public void run() {
					mHandler.sendEmptyMessage(PROGRESS_WAIT_VISIBLE);
					Bitmap tmpBitmap = new LomoFilter(mBitmap).imageProcess()
							.getDstBitmap();
					Message msg = new Message();
					msg.obj = tmpBitmap;
					msg.arg1 = 7;
					msg.what = PROGRESS_WAIT_GONE;
					mHandler.sendMessage(msg);
				}
			}.start();
			break;
		case R.id.iv_yuantu:
		//	imageView2.setImageBitmap(mBitmap);
			tmpBitmap=mBitmap;
			imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
			//mYuanTu.setBackgroundResource(R.drawable.bg_fiter2);
			break;
		}
	}
	/**
	 * 保存tupian
	 */
	private void load(){
		String name = DateFormat.format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".jpg";   
        Toast.makeText(this, name, Toast.LENGTH_LONG).show();  
       
      
        FileOutputStream b = null;           
        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/TianGouPic/");  
        file.mkdirs();// 创建文件夹  
         FITERNAME = Environment.getExternalStorageDirectory().getPath()+"/TianGouPic/"+name;  
        
        try {  
            b = new FileOutputStream(FITERNAME);  
            tmpBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                b.flush();  
                b.close();
               /* ActCardDetail.FiterHander.sendEmptyMessage(HandlerWhat);
                Message msg = new Message();
                msg.obj=FITERNAME;
                ActCardDetail.FiterHander.sendMessage(msg);*/
                setBroadcast();
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
      
	}
	private void setBroadcast(){
		Intent intent = new Intent();
		   intent.setAction("com.tiangou.aaaa");
		   //要发送的内容
		   intent.putExtra("fitername", FITERNAME);
		   //发送 一个无序广播
		  this.sendBroadcast(intent);
	}
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case PROGRESS_WAIT_VISIBLE:
				setProgressBarIndeterminateVisibility(true);
				break;
			case PROGRESS_WAIT_GONE:
				setProgressBarIndeterminateVisibility(false);
				Bitmap tmpBitmap = (Bitmap) msg.obj;
				//imageView2.setImageBitmap(tmpBitmap);
				imageView2.setBackgroundDrawable(ImageUtil.BitmapToDrawable(tmpBitmap));
				ImageCache.put(key[msg.arg1], tmpBitmap);
				break;
			default:
				break;
			}
		}
	};
	private final int PROGRESS_WAIT_VISIBLE = 1;
	private final int PROGRESS_WAIT_GONE = 2;

}
